import ctypes

PINEA_EVENT_QUIT = 0x100
PINEA_EVENT_KEY_DOWN = 0x300
PINEA_EVENT_KEY_UP = 0x301
PINEA_EVENT_MOUSE_MOTION = 0x400
PINEA_EVENT_MOUSE_BUTTON_DOWN = 0x401
PINEA_EVENT_MOUSE_BUTTON_UP = 0x402
PINEA_EVENT_MOUSE_WHEEL = 0x403

PINEA_KEY_SPACE = 0x00000020
PINEA_KEY_A = 0x00000061
PINEA_KEY_D = 0x00000064
PINEA_KEY_S = 0x00000073
PINEA_KEY_W = 0x00000077
PINEA_KEY_H = 0x00000068
PINEA_KEY_J = 0x0000006a
PINEA_KEY_K = 0x0000006b
PINEA_KEY_L = 0x0000006c

PINEA_FLIP_HORIZONTAL = 0x00000001
PINEA_FLIP_VERTICAL = 0x00000002

# 定义结构体
class PineaRect(ctypes.Structure):
    _fields_ = (
        ("x", ctypes.c_float),
        ("y", ctypes.c_float),
        ("w", ctypes.c_float),
        ("h", ctypes.c_float)
    )

class PineaColor(ctypes.Structure):
    _fields_ = (
        ("r", ctypes.c_uint8),
        ("g", ctypes.c_uint8),
        ("b", ctypes.c_uint8),
        ("a", ctypes.c_uint8)
    )

class PineaEvent(ctypes.Structure):
    _fields_ = (
        ("type", ctypes.c_int),
        ("key", ctypes.c_int),
        ("x", ctypes.c_int),
        ("y", ctypes.c_int),
    )

# 加载动态链接库
PineaLib = ctypes.CDLL("./Pinea/Pinea/bin/Pinea.dll")

# 窗口相关函数绑定
PineaLib.pineaInit.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int]
PineaLib.pineaInit.restype = None
def pineaInit(title, width, height):
    return PineaLib.pineaInit(title.encode('utf-8'), width, height)

PineaLib.pineaQuit.argtypes = []
PineaLib.pineaQuit.restype = None
def pineaQuit():
    return PineaLib.pineaQuit()

PineaLib.pineaShow.argtypes = []
PineaLib.pineaShow.restype = None
def pineaShow():
    return PineaLib.pineaShow()

# 事件处理函数绑定
PineaLib.pineaPollEvent.argtypes = [ctypes.POINTER(PineaEvent)]
PineaLib.pineaPollEvent.restype = ctypes.c_bool
def pineaPollEvent(event):
    return PineaLib.pineaPollEvent(ctypes.byref(event))

# 渲染相关函数绑定
PineaLib.pineaClear.argtypes = [ctypes.POINTER(PineaColor)]
PineaLib.pineaClear.restype = None
def pineaClear(color):
    return PineaLib.pineaClear(ctypes.byref(color))

PineaLib.pineaRender.argtypes = []
PineaLib.pineaRender.restype = None
def pineaRender():
    return PineaLib.pineaRender()

# 图像绘制函数绑定
PineaLib.pineaFillRect.argtypes = [ctypes.POINTER(PineaRect), ctypes.POINTER(PineaColor)]
PineaLib.pineaFillRect.restype = None
def pineaFillRect(rect, color):
    return PineaLib.pineaFillRect(ctypes.byref(rect), ctypes.byref(color))

PineaLib.pineaLoadImage.argtypes = [ctypes.c_char_p]
PineaLib.pineaLoadImage.restype = ctypes.c_void_p
def pineaLoadImage(path):
    return PineaLib.pineaLoadImage(path.encode('utf-8'))

PineaLib.pineaDrawImage.argtypes = [ctypes.c_void_p, ctypes.POINTER(PineaRect), 
                                   ctypes.POINTER(PineaRect), ctypes.c_double, ctypes.c_int]
PineaLib.pineaDrawImage.restype = None
def pineaDrawImage(image, srcRect, dstRect, angle, flip):
    src = ctypes.byref(srcRect) if srcRect else None
    dst = ctypes.byref(dstRect) if dstRect else None
    return PineaLib.pineaDrawImage(image, src, dst, angle, flip)

PineaLib.pineaDropImage.argtypes = [ctypes.c_void_p]
PineaLib.pineaDropImage.restype = None
def pineaDropImage(image):
    return PineaLib.pineaDropImage(image)

# 音频相关函数绑定
PineaLib.pineaLoadMusic.argtypes = [ctypes.c_char_p]
PineaLib.pineaLoadMusic.restype = ctypes.c_void_p
def pineaLoadMusic(path):
    return PineaLib.pineaLoadMusic(path.encode('utf-8'))

PineaLib.pineaPlayMusic.argtypes = [ctypes.c_void_p, ctypes.c_bool]
PineaLib.pineaPlayMusic.restype = None
def pineaPlayMusic(audio, loop):
    return PineaLib.pineaPlayMusic(audio, loop)

PineaLib.pineaPauseMusic.argtypes = []
PineaLib.pineaPauseMusic.restype = None
def pineaPauseMusic():
    return PineaLib.pineaPauseMusic()

PineaLib.pineaResumeMusic.argtypes = []
PineaLib.pineaResumeMusic.restype = None
def pineaResumeMusic():
    return PineaLib.pineaResumeMusic()

PineaLib.pineaStopMusic.argtypes = []
PineaLib.pineaStopMusic.restype = None
def pineaStopMusic():
    return PineaLib.pineaStopMusic()

PineaLib.pineaDropMusic.argtypes = [ctypes.c_void_p]
PineaLib.pineaDropMusic.restype = None
def pineaDropMusic(audio):
    return PineaLib.pineaDropMusic(audio)

PineaLib.pineaLoadSound.argtypes = [ctypes.c_char_p]
PineaLib.pineaLoadSound.restype = ctypes.c_void_p
def pineaLoadSound(path):
    return PineaLib.pineaLoadSound(path.encode('utf-8'))

PineaLib.pineaPlaySound.argtypes = [ctypes.c_void_p, ctypes.c_int]
PineaLib.pineaPlaySound.restype = None
def pineaPlaySound(audio, loops):
    return PineaLib.pineaPlaySound(audio, loops)

PineaLib.pineaDropSound.argtypes = [ctypes.c_void_p]
PineaLib.pineaDropSound.restype = None
def pineaDropSound(audio):
    return PineaLib.pineaDropSound(audio)

# 时间相关函数绑定
PineaLib.pineaGetTime.argtypes = []
PineaLib.pineaGetTime.restype = ctypes.c_uint64
def pineaGetTime():
    return PineaLib.pineaGetTime()
